﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace XinChen
{
    class UK7_Admin
    {
        private static int instance = 0;//计算实例化的总数量
        /// <summary>
        /// 是否已经初始化成功，
        /// 读取属性时，如果失败，Ready也会变为False。属性会返回“ErrCode:错误码”
        /// </summary>
        public bool Ready { get; protected set; }

        public UK7_Admin()
        {
            UK7_Admin.instance++;
            Ready = (UK7_Admin.instance <= 1);
        }
        /// <summary>
        /// 析构，计数器减一，锁Logout()。
        /// </summary>
        ~UK7_Admin()
        {
            UK7_Admin.instance--;
            //释放加密锁
        }

        /// <summary>
        /// 磐石NT77管理类，http://www.panshitech.com/Download/NT77.htm
        /// </summary>
        public static class NT77Admin
        {
            //查找所有加密锁，最多支持同时8只狗，NTHandles应该初始化为int[8]，nKeyCount是整数指针，暂时用int[1]解决。
            [DllImport("Lib\\UK7_Admin.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
            public static extern int NTFindAll(int[] NTHandle, int[] nKeyCount);

            //获取加密锁硬件属性
            [DllImport("Lib\\UK7_Admin.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
            public static extern int NTGetDevCaps(int NTHandle, int keyMode, int keyVersion, int e2size, int ramSize, int productionTime);

            //获取硬件序列号
            [DllImport("Lib\\UK7_Admin.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
            public static extern int NTGetHardwareID(int NTHandle, StringBuilder hardwareID);

            //检测管理密码（管理员登陆）
            [DllImport("Lib\\UK7_Admin.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
            public static extern int NTCheckSuperPin(int NTHandle, string superPin);
            //修改加密锁管理密码
            [DllImport("Lib\\UK7_Admin.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
            public static extern int NTSetSuperPin(int NTHandle, string newPin);

            //修改加密锁登陆密码(通过管理密码和种子码生成 用户密码)
            [DllImport("Lib\\UK7_Admin.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
            public static extern int NTSetUserPin(int NTHandle, string superPin, string seed, StringBuilder userPin);

            //修改加密锁识别码
            [DllImport("Lib\\UK7_Admin.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
            public static extern int NTSetAppName(int NTHandle, string NTCode);
            //获取加密锁识别码
            [DllImport("Lib\\UK7_Admin.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
            public static extern int NTGetAppName(int NTHandle, StringBuilder NTCode);

            //读取存储区数据
            [DllImport("Lib\\UK7_Admin.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
            public static extern int NTRead(int NTHandle, int address, int Length, byte[] pData);
            //写入存储区数据
            [DllImport("Lib\\UK7_Admin.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
            public static extern int NTWrite(int NTHandle, int address, int Length, byte[] pData);
        }

        /// <summary>
        /// 加密锁数据，用于生产加密锁
        /// </summary>
        public class UK_Info
        {
            static string DefaultAdmin = "Admin";
            static string DefaultProgID = "1234567890ABCDEF";

            string AdminPin;
            string Seed;
            string UserPin;
            string ProgID;
            byte[] Data = new byte[1024];

            public byte[] toBin(int key)
            {
                return null;
            }
            public bool fromBin(byte[] bin, int key)
            {
                return false;
            }
        }
    

    }
}